package com.example.demo2.controller;

import com.example.demo2.biz.Userbiz;
import com.example.demo2.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("User")
public class UserController {
    private @Autowired Userbiz userbiz;
    @RequestMapping("saveUser")
    public User saveStudent(String nickName,String openID,String gender,String Province,String city,String avatarUrl){
        String result = "";
        Date time = new Date();
        String dateStr1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
         System.out.println(Province);
        User user = new User(nickName,openID,gender,Province,city,avatarUrl);
        try {
            if(userbiz.getUser(openID)==null)
                userbiz.saveTnStudent(user);
            else
                userbiz.updateUser(user);
            result = "插入成功";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = "插入失败";
        }
        return user;

    }

    @RequestMapping("selectuser")
    public User selectuser(String openID) throws Exception {
           User user =userbiz.getUser(openID);
       return user;
    }
    @RequestMapping("selectalluser")
    public List<User> selectalluser() throws Exception {
        List<User> user =userbiz.getallUser();
        return user;
    }

}